package aenv;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

public class Automata implements Iterator<Integer>, Iterable<Integer>{
		int transitionMatrix[][];
		HashMap <Character, Integer> alphabet;
		int currentState; 
		char [] inputString;
		int currentCharacter;
		public Automata(int alphabetSize, int statesSize, int initialState, char inputString[])
		{
			transitionMatrix = new int[statesSize][alphabetSize];
			alphabet = new HashMap <Character, Integer>(alphabetSize);
			currentState = initialState; //set initial state
			this.inputString = inputString;
			currentCharacter = 0;
		}
		public void setAlphabet(List <Character> arrayAlphabet)
		{
			int charToInt = 0;
			for (Character symbol : arrayAlphabet)
			{
				alphabet.put(symbol, charToInt);
				charToInt++;
			}
		}
		public void setSingleTransition(Integer state, Character symbol, Integer newState)
		{
			transitionMatrix[state][alphabet.get(symbol)]=newState;
		}
		public int singleTransition (Character symbol)
		{
			return currentState = transitionMatrix[currentState][alphabet.get(symbol)];
		}
		@Override
		public boolean hasNext() {
			// TODO Auto-generated method stub
			return currentCharacter < inputString.length;
		}
		@Override
		public Integer next() {
			// TODO Auto-generated method stub
			if (hasNext()) return singleTransition(inputString[currentCharacter++]);
			
			throw new NoSuchElementException();
		}
		@Override
		public void remove() {
			// TODO Auto-generated method stub
			throw new UnsupportedOperationException();
		}
		@Override
		public Iterator<Integer> iterator() {
			// TODO Auto-generated method stub
			return this;
		}
		
}